﻿Public Class DockPanel : Inherits UIPanel

	Public Overrides Sub Resize(Rectangle As System.Drawing.Rectangle)
		MyBase.Resize(Rectangle)
		Dim R = ContentRectangle
		For Each Child In Children
			If Child Is Children.Last Then
				Child.Resize(R)
			Else
				Select Case Child.Dock
					Case Docking.Top
						Child.Resize(New Rectangle(R.Left, R.Top, R.Width, Child.Height))
						R.Height -= Child.Height
						R.Offset(0, Child.Height)
					Case Docking.Left
						Child.Resize(New Rectangle(R.Left, R.Top, Child.Width, R.Height))
						R.Width -= Child.Width
						R.Offset(Child.Width, 0)
					Case Docking.Right
						Child.Resize(New Rectangle(R.Right - Child.Width, R.Top, Child.Width, R.Height))
						R.Width -= Child.Width
				End Select
			End If
		Next
	End Sub

	Public Overrides ReadOnly Property Height As Integer
		Get
			Return MyBase.Height + MinHeight()
		End Get
	End Property

	Public Overrides ReadOnly Property Width As Integer
		Get
			Return MyBase.Width + MinWidth()
		End Get
	End Property

	Function MinWidth() As Integer
		Return Math.Max((From c In Children Select c.Width).Max, (From c In Children Where c.Dock = Docking.Left Or c.Dock = Docking.Right Or c.Dock = Docking.None Select c.Width).Sum)
	End Function

	Function MinHeight() As Integer
		Return Math.Max((From c In Children Select c.Height).Max, (From c In Children Where c.Dock = Docking.Top Or c.Dock = Docking.Bottom Or c.Dock = Docking.None Select c.Height).Sum)
	End Function

End Class
